import {ChangeDetectionStrategy, Component, OnInit} from '@angular/core';
import {BasicConfirmModalComponent} from '../../base-modal';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {ContractLabel, OptionsInterface} from '../../../core/services/types';
import {NzModalRef} from 'ng-zorro-antd/modal';
import {ValidatorsService} from '../../../core/services/validators/validators.service';
import {Observable, of} from 'rxjs';
import {fnCheckForm} from '../../../utils/tools';
import {LabelService} from '../../../core/services/http/label.service';
import {FileTypeService} from '../../../core/services/http/file-type.service';
import {NzMessageService} from "ng-zorro-antd/message";

@Component({
  selector: 'app-file-manage-modal',
  templateUrl: './file-manage-modal.component.html',
  styleUrls: ['./file-manage-modal.component.less'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class FileManageModalComponent extends BasicConfirmModalComponent implements OnInit {
  addEditForm!: FormGroup;
  params!: ContractLabel;
  isEdit = false;
  labelOptions: OptionsInterface[] = [];
  isDisplay!: boolean;

  constructor(private modalRef: NzModalRef, private fb: FormBuilder,
              private fileTypeService: FileTypeService,
              private validatorsService: ValidatorsService, private labelService: LabelService, private message: NzMessageService) {
    super();
  }


  getLabelsList(): Promise<void> {
    return new Promise<void>(resolve => {
      this.labelService.getTypeList({pageSize: 0, pageNum: 0}).subscribe(({list}) => {
        this.labelOptions = [];
        list.forEach(({id, typeName}) => {
          const obj: OptionsInterface = {
            label: typeName,
            value: id!,
          };
          this.labelOptions.push(obj);
        });
        resolve();
      });
    });
  }

  show(e: boolean): void {
    this.isDisplay = e;
  }

  initForm(): void {
    this.addEditForm = this.fb.group({
      typeId: [null, [Validators.required, Validators.maxLength(255)]],
      typeName: [null, [Validators.maxLength(255)]],
      lendFlag: [false, [Validators.required, Validators.maxLength(255)]],
      projectNum: [null, [Validators.required, Validators.maxLength(255)]],
      client: [null, [Validators.required, Validators.maxLength(255)]],
      projectContent: [null, [Validators.required, Validators.maxLength(255)]],
      signPerson: [null, [Validators.maxLength(255)]],
      lendTime: [null, [Validators.maxLength(255)]],
      lendPerson: [null, [Validators.maxLength(255)]],
      evaluatePerson: [null, [Validators.maxLength(255)]],
      startTaskTime: [null, [Validators.maxLength(255)]],
      remark: [null, [Validators.maxLength(255)]],
    });
  }

  async ngOnInit(): Promise<void> {
    this.initForm();
    await this.getLabelsList();
    if (Object.keys(this.params).length > 0) {
      this.isEdit = true;
      this.addEditForm.patchValue(this.params);
    }
  }

  // 此方法为如果有异步数据需要加载，则在该方法中添加
  protected getAsyncFnData(modalValue: any): Observable<any> {
    return of(modalValue);
  }

  // 返回false则不关闭对话框
  protected getCurrentValue(): Observable<any> {
    //  console.log(this.addEditForm);
    if (!fnCheckForm(this.addEditForm)) {
      return of(false);
    }
    return of(this.addEditForm.value);
  }
}
